package cc.comac.mom.datahandlers.entity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.QualityProgressExplain;
import cc.comac.mom.bean.base.LimitPageBean;
import cc.comac.mom.dao.entity.QualityProgressExplainDao;
import cc.comac.mom.entity.QualityProgressExplainEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
public class QualityProgressExplainDataHandler extends ServiceImpl<QualityProgressExplainDao, QualityProgressExplainEntity>{

	@Autowired
	private QualityProgressExplainDao QualityProgressExplainDao;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	public ResultBean insert(QualityProgressExplain obj) {
		ResultBean resultBean = new ResultBean(true);
		if(obj == null || obj.getInnerData() == null) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}
		
		if(obj.getInnerData().getTestNo() == null || "".equals(obj.getInnerData().getTestNo())) {
			resultBean.setErrorMessge("实验编号不能为空");
			return resultBean;
		}
		
		baseEntityDataUtil.setBaseEntityData(obj.getInnerData());
		int insert = QualityProgressExplainDao.insert(obj.getInnerData());
		if(insert <1) {
			resultBean.setErrorMessge("进度信息插入失败");
			return resultBean;
		}
		return resultBean;
		
	}
	
	public LimitPageBean<QualityProgressExplainEntity> selectByCondition(QualityProgressExplain obj) {
    	QueryWrapper<QualityProgressExplainEntity> queryWrapper = new QueryWrapper<QualityProgressExplainEntity>();
    	queryWrapper.eq("row_status",1);
    	if(obj.getPageSize() == 0) {
    		obj.setPageSize(10);
    	}
		if(obj != null && obj.getInnerData() != null) {		
    		if(obj.getInnerData().getTestNo() != null && !"".equals(obj.getInnerData().getTestNo())) {
    			queryWrapper.like("test_no",obj.getInnerData().getTestNo());
    		}
    		if(obj.getInnerData().getProgressExplain() != null && !"".equals(obj.getInnerData().getProgressExplain())) {
    			queryWrapper.like("progress_explain",obj.getInnerData().getProgressExplain());
    		}
    		if(obj.getInnerData().getStatus() != null && !"".equals(obj.getInnerData().getStatus())) {
    			queryWrapper.like("status",obj.getInnerData().getStatus());
    		}
		}
		Page<QualityProgressExplainEntity> page = new Page<>(obj.getPageNum(),obj.getPageSize(),true);
		IPage<QualityProgressExplainEntity> iPage = QualityProgressExplainDao.selectPage(page, queryWrapper);
		@SuppressWarnings("unchecked")
		LimitPageBean<QualityProgressExplainEntity> limitPageBean = (LimitPageBean<QualityProgressExplainEntity>) LimitPageBean.of(iPage);
		return limitPageBean;
		
	}
}
